-- UIDungeonTiedPig
-- Created by panyl Apr/29/2015
-- 被捆绑的野猪界面

UIDungeonTiedPig = class("UIDungeonTiedPig", function()
    return cc.Layer:create();
end);

function UIDungeonTiedPig.create(pos)
    return UIDungeonTiedPig.new(pos);
end

function UIDungeonTiedPig:ctor(pos)
    -- 初始化
    self:setName("UIDungeonTiedPig");
    local node = cc.CSLoader:createNode("layout/dungeon/DungeonTiedPig.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.pos = pos;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIDungeonTiedPig:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIDungeonTiedPig:registerEventCallback()
    -- 关注解救野猪事件
    EventMgr.register("UIDungeonTiedPig", event.RESCUE_PIG, function(args)
        local list;
        if args["result"] == false then
            -- 解救了，但不能召唤
            list = splitString(getLocStr("rescue_tied_pig2"), "\n");
        else
            -- 成功召唤
            list = splitString(getLocStr("rescue_tied_pig"), "\n");
        end
        splitAlert(list, 1);

        -- 关闭界面
        UIMgr.getCurrentScene():removeFormByName("UIDungeonTiedPig");
    end);

    -- 关注烧烤事件
    EventMgr.register("UIDungeonTiedPig", event.PIG_BARBECUE, function()
        alert(getLocStr("pig_barbecue_tip"));

        -- 关闭界面
        UIMgr.getCurrentScene():removeFormByName("UIDungeonTiedPig");
    end);

    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIDungeonTiedPig 界面析构清理");

            EventMgr.removeAll("UIDungeonTiedPig");

            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIDungeonTiedPig:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIDungeonTiedPig");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    local kerning = -3;
    if getLang() == "en" then
        kerning = -1;
    elseif getLang() == "fr" then
        kerning = -1;
    end

    -- 继续烧烤
    local btnBBQ = findChildByName(node, "CT/btn_barbecue");
    local pos = self.pos;
    local function okClickBBQ(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- TODO: 烤猪
            EventMgr.fire(event.PIG_BARBECUE);

            DungeonActionM.go("take_instance_bonus", pos);
        end
    end
    btnBBQ:addTouchEventListener(okClickBBQ);
    TextStyleM.setTextStyle(btnBBQ, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, kerning);
    btnBBQ:setTitleText(string.format(getLocStr("pig_barbecue_btn"), btnBBQ.wager));

    -- 解救野猪
    local btnRescue = findChildByName(node, "CT/btn_rescue");
    local function okClickRescue(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");

            -- 解救野猪
            DungeonActionM.go("rescue_pig", pos);
        end
    end
    btnRescue:addTouchEventListener(okClickRescue);
    TextStyleM.setTextStyle(btnRescue, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, kerning);
    btnRescue:setTitleText(string.format(getLocStr("rescue_pig_btn"), btnRescue.wager));
end

-- 重绘
function UIDungeonTiedPig:redraw()
    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");

    local gridData = DungeonM.getGridByPos(self.pos);
    local info = BuildingBonusM.queryBuildingView(gridData.class);

    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    applyString(descLabel, info["dialog"]);
    titleLabel:setString(info["title"]);
    iconImg:loadTexture(getLevelItemIconPath(info["icon"]));

    iconImg:setScale(1.5);
    TextStyleM.setArtWordTitleStyle(titleLabel);
end

